/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import dbHandling.DatabaseConnect;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import jpamodel.Category;
import jpamodel.CommissionWare;
import jpamodel.OwnedWare;
import jpamodel.Seller;

/**
 *
 * @author Hans
 */
public class ShopFacade {

    private DatabaseConnect dbc;
    private ObservableList<CommissionWare> wares;
    private ObservableList<OwnedWare> ownedWares;
    private ObservableList<Category> categories;
    private ObservableList<Category> categoriesAllExcluded;
    private ObservableList<Seller> sellers;
    private EntityManager em;

    public ShopFacade(DatabaseConnect dbc) {
        this.dbc = dbc;
        categories = FXCollections.observableArrayList();
        categoriesAllExcluded = FXCollections.observableArrayList();
        wares = FXCollections.observableArrayList();
        ownedWares = FXCollections.observableArrayList();
        sellers = FXCollections.observableArrayList();
        dbc.getEntityManager();
    }

    public ObservableList<Category> getCategories(boolean allExcluded) {
        if (checkEM() && categories.isEmpty()) {

            categories = FXCollections.observableArrayList();
            categories.clear();
            categoriesAllExcluded.clear();
            TypedQuery<Category> query1 = em.createNamedQuery("Category.findAll", Category.class);
            categories.addAll(query1.getResultList());
            for (int i = 1; i < categories.size(); i++) {
                categoriesAllExcluded.add(categories.get(i));
            }

        }
        return (allExcluded) ? categoriesAllExcluded : categories;
    }

    public ObservableList<CommissionWare> getAllCommissionWares() {
        if (checkEM() && wares.isEmpty()) {
            wares.clear();
            TypedQuery<CommissionWare> query1 = em.createNamedQuery("CommissionWare.findAll", CommissionWare.class);
            wares.addAll(query1.getResultList());
        }
        return wares;
    }

    public ObservableList<OwnedWare> getAllOwnedWares() {
        if (checkEM() && ownedWares.isEmpty()) {
            checkEM();
            ownedWares.clear();
            TypedQuery<OwnedWare> query1 = em.createNamedQuery("OwnedWare.findAll", OwnedWare.class);
            ownedWares.addAll(query1.getResultList());
        }
        return ownedWares;
    }

    public ObservableList<Seller> getAllSellers() {
        if (checkEM() && sellers.isEmpty()) {
            sellers.clear();
            TypedQuery<Seller> query1 = em.createNamedQuery("Seller.findAll", Seller.class);
            sellers.addAll(query1.getResultList());
        }
        return sellers;
    }

    private boolean checkEM() {
        if (dbc.isConnected() && (em == null || !em.isOpen())) {
            em = dbc.getEntityManager();
        }
        return em != null && em.isOpen();
    }

    public DatabaseConnect getDBC() {
        return dbc;
    }

    public void persist(Object object) {
        if (checkEM()) {
            em.getTransaction().begin();
            em.persist(object);
            em.getTransaction().commit();
            if (object instanceof Seller) {
                Seller s = (Seller) object;
                if (!sellers.contains(s)) {
                    sellers.add(s);
                }
            } else if (object instanceof CommissionWare) {
                CommissionWare w = (CommissionWare) object;
                if (!wares.contains(w)) {
                    wares.add(w);
                }
            } else if (object instanceof OwnedWare) {
                OwnedWare w = (OwnedWare) object;
                if (!ownedWares.contains(w)) {
                    ownedWares.add(w);
                }
            }
        }
    }

    public void remove(Object object) {
        if (checkEM()) {
            em.getTransaction().begin();
            em.remove(object);
            em.getTransaction().commit();

        }
    }

    public void close() {
        dbc.close();
    }

    public void updateAll() {
        if (checkEM()) {
            getAllCommissionWares();
            getAllOwnedWares();
            getAllSellers();
            getCategories(true);
        }
    }

}
